package demo;

import javax.persistence.*;

import org.mortbay.log.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeleteGeofence extends HttpServlet{
	private static final Logger log = Logger.getLogger(MyTrackServlet.class
			.getName());
	EntityManager em;
	    
	    private String imei;
	    private String fenceType;
	    private String alert;
	    
	    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
	        //MyClass myClass = new MyClass();
	    	imei = request.getSession().getAttribute("imei").toString();

	        if (request.getParameter("fenceType") != null) {
	        	fenceType = request.getParameter("fenceType");
	        }
	        
	        String PermanentType = "Permanent";
	        String TemporaryType = "Temporary";
	        String ClearAlert = "ClearAlert";
	        String Clear = "clear";
	        try{
	        if (fenceType.equals(PermanentType)){
	        	DeletePermanentFenceData();
 				PrintWriter out = response.getWriter();
 		        out.println("<html><body onLoad=\"alert('Geofence Cleared');window.location.href='http://omnihtrack.appspot.com/index.html';\"></html>");
	        }
	        if (fenceType.equals(TemporaryType)){
	 	        	DeleteTemporaryFenceData();
	 				PrintWriter out = response.getWriter();
	 		        out.println("<html><body onLoad=\"alert('Geofence Cleared');window.location.href='http://omnihtrack.appspot.com/index.html';\"></html>");

	 	    }
	        if (fenceType.equals(ClearAlert)){
	        	DeleteAllAlerts();
				PrintWriter out = response.getWriter();
		        out.println("<html><body onLoad=\"alert('All Alerts Cleared');window.location.href='http://omnihtrack.appspot.com/index.html';\"></html>");

	        }
	        }
	        catch(Exception ex){
				PrintWriter out = response.getWriter();
		        out.println("<html><body onLoad=\"alert('Geofence Cleared');window.location.href='http://omnihtrack.appspot.com/index.html';\"></html>");
	        }

	        
	        
    		//response.setContentType("text/plain");
    		//response.getWriter().println("OK" + " " + imei + " " + distance + " " + latitude + " " + longitude);

    	}
		public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws IOException {
			doGet(request,response);
		}
		
		public void DeleteAllAlerts() {
			em = EMF.get().createEntityManager();
			
			Query q = em.createQuery("delete from OutsideGeofence d where imei = '" + imei + "'");
			//q.setParameter("subdate", subdate);
			int i = q.executeUpdate();
			//System.out.println("Deleted: " + i);
			em.close();
		}
		

		public void DeleteTemporaryFenceData() {

			//Date subdate = getCurre();
			
			em = EMF.get().createEntityManager();
			
			Query q = em.createQuery("delete from TemporaryGeofenceData d where imei = '" + imei + "'");
			//q.setParameter("subdate", subdate);
			int i = q.executeUpdate();
			//System.out.println("Deleted: " + i);
			em.close();
		}
		
		public void DeletePermanentFenceData() {

			//Date subdate = getCurre();
			
			em = EMF.get().createEntityManager();
			
			Query q = em.createQuery("delete from PermanentGeofenceData d where imei = '" + imei + "'");
			//q.setParameter("subdate", subdate);
			int i = q.executeUpdate();
			//System.out.println("Deleted: " + i);
			em.close();
		}

	    



}
